//
//  DeleteImgViewController.m
//  u76ho
//
//  Created by m-air-01 on 16/2/22.
//  Copyright © 2016年 LV. All rights reserved.
//

#import "DeleteImgViewController.h"
#import "BoostCollectionViewCell.h"

@interface DeleteImgViewController ()
<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIActionSheetDelegate>
{
    UICollectionView * _collectionView;
    NSMutableArray * dataSource;
    CGSize size;
}

@end

@implementation DeleteImgViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self main];
   // [self addBackItem];
    [self addItem];
    // Do any additional setup after loading the view.
}

-(void)main
{
    self.view.backgroundColor = [UIColor blackColor];
    dataSource = [[NSMutableArray alloc]init];
    [dataSource addObjectsFromArray:_imgArr];
    size = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT-64);
    UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc]init];
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) collectionViewLayout:flow];
    _collectionView.showsHorizontalScrollIndicator = 0;
    _collectionView.showsVerticalScrollIndicator = 0;
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.backgroundColor = [UIColor blackColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"BoostCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"BoostCollectionViewCell"];
    _collectionView.contentOffset = CGPointMake(SCREEN_WIDTH*(_i - 1), 0);
    [self.view addSubview:_collectionView];
    
    self.navigationItem.title = [NSString stringWithFormat:@"%i/%i",(int)_i,(int)dataSource.count];
    
}
#pragma mark - _collectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return dataSource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BoostCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BoostCollectionViewCell" forIndexPath:indexPath];
    
    cell.img = [dataSource objectAtIndex:indexPath.item];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return size;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.navigationItem.title = [NSString stringWithFormat:@"%i/%i",(int)(scrollView.contentOffset.x/SCREEN_WIDTH)+1,(int)dataSource.count];
    _i = (int)(scrollView.contentOffset.x/SCREEN_WIDTH)+1;
}
#pragma mark - addItems
-(void)addItem
{
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [btn setTitle:@"删除" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}
-(void)deleteBtnAction:(UIButton *)btn
{
    UIActionSheet * sheet = [[UIActionSheet alloc]initWithTitle:@"要删除这张照片吗?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"删除", nil];
    [sheet showInView:self.view];
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [dataSource removeObjectAtIndex:_i-1];
        [_collectionView reloadData];
        if (_i == dataSource.count+1) {
            self.navigationItem.title = [NSString stringWithFormat:@"%i/%i",(int)_i-1,(int)dataSource.count];
        }
        else
        {
            self.navigationItem.title = [NSString stringWithFormat:@"%i/%i",(int)_i,(int)dataSource.count];
        }
        _i = _i - 1;
        if (dataSource.count == 0) {
            [self.delegate deleteFinishImgArr:dataSource];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}
#pragma  mark - 返回按钮图标
-(void)addBackItem{
    UIButton * backItem = [UIButton buttonWithType:UIButtonTypeCustom];
    backItem.frame = CGRectMake(0, 0, 9, 17);
    [backItem setImage:[UIImage imageNamed:@"返回图标"] forState:UIControlStateNormal];
    [backItem addTarget:self action:@selector(backItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backItem];
}
-(void)backItem:(id)sender
{
    [self.delegate deleteFinishImgArr:dataSource];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
